package com.zd.commodity.productMng.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.zd.commodity.action.BaseAction;
import com.zd.commodity.brandMng.entity.BrandShow;
import com.zd.commodity.categoryMng.entity.Category;
import com.zd.commodity.dto.ProductAttrExcel;
import com.zd.commodity.productMng.biz.IProductAttrBiz;
import com.zd.commodity.productMng.biz.IProductAttrDictBiz;
import com.zd.commodity.productMng.entity.ProductAttr;
import com.zd.commodity.productMng.entity.ProductAttrDict;
import com.zd.commodity.productMng.entity.ProductAttrGroup;

@Scope("prototype")
@Controller("productAttrAction")
public class ProductAttrAction extends BaseAction<ProductAttr> {

	private IProductAttrBiz productAttrBiz;
	private String attrGroupName;
	public String showAttrPage(){
		ActionContext.getContext().put("isShowBlank", true);
		return "list";
	}
	public IProductAttrBiz getProductAttrBiz() {
		return productAttrBiz;
	}
	private IProductAttrDictBiz productAttrDictBiz;

	public IProductAttrDictBiz getProductAttrDictBiz() {
		return productAttrDictBiz;
	}
	@Autowired
	public void setProductAttrDictBiz(IProductAttrDictBiz productAttrDictBiz) {
		this.productAttrDictBiz = productAttrDictBiz;
	}
	@Autowired
	public void setProductAttrBiz(IProductAttrBiz productAttrBiz) {
		this.productAttrBiz = productAttrBiz;
		this.setBaseBiz(productAttrBiz);
	}
	
	public void excelExport() throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		ProductAttr productAttr=super.getT1();
		map.put("name", productAttr.getName());
		map.put("categoryId", productAttr.getCategory().getId().toString());
		
		List list = productAttrBiz.getExcelExportListBySql(ProductAttrExcel.class,"productAttrExcelExport",map.size()>0?map:null, -1, -1);
		
		String[] title = new String[]{"属性名称","四级类别名称","是否为属性键","创建时间","创建人"};
		String[] neededFields = new String[]{"name","categoryName","isKeyAttr","creationTime","creator"};
		super.excelExport("商品属性管理", list,title,neededFields);
	}
	
	public void downloadCheckTemple() throws Exception{
		String[] title = new String[]{"商品名称","型号","品牌名称","分类名称","属性组名称","属性名称","属性值","供应商名称","校验结果"};
		super.excelExport("属性校验上传模板", new ArrayList(),title,null);
	}
	/**
	 * 下载属性模板
	 */
	public void downloadTemplet() throws Exception{
		List brandTemplet = productAttrBiz.getExcelExportListBySql(BrandShow.class, "downloadTemplet", null, 0, 0);
		String[] title = new String[]{"属性名称","4级分类名称","是否为主要键","原始值","标准值","简称"};
		String[] neededFields = new String[]{"name","category_id","is_key_attr"};
		super.excelExport("属性上传模板", brandTemplet,title,neededFields);
	}
	/**
	 * 模板导入
	 * @throws Exception 
	 */
	public void uploadExeclData() throws Exception{
		String message  = "上传成功";
		HashMap<Integer, String> map = new HashMap<Integer ,String>();
		String queryTable = "product_attr a LEFT JOIN category c ON a.category_id = c.id LEFT JOIN product_attr_group g ON g.id = a.group_id";
		map.put(0, "queryNameByIdorlist|id|category c|c.name");
		map.put(1, "queryNameByIdorlist|id|product_attr_group g|g.name");
		map.put(2, "queryNameByIdorlist|a.id,c.id,g.id|"+queryTable+"|a.name-c.id#0-g.id#1");
		List<String[]> data = productAttrBiz.readExecl(map);
		if(data.size()==1){
			String[] strings = data.get(0);
			if("0".equals(strings[0])){
				message = strings[1];
			}
		}
		if(message.equals("上传成功")){
			//删除字典或添加属性
			for (String[] strings : data) {
				//如果有该属性，删除其下的字典
				if(strings[2].contains("#id")){
					String[] split = strings[2].split("#id");
					System.out.println(split[0]);
					strings[2]=split[0];
					productAttrDictBiz.deleteAttrDictByAttrId(strings[2]);
				}else{
					//如果没有该属性
					ProductAttr productAttr = new ProductAttr();
					Category category = new Category();
					category.setId(Long.valueOf(strings[0]));
					productAttr.setCategory(category);
					productAttr.setGroupId(Long.valueOf(strings[1]));
					productAttr.setAttrDicts(null);
					productAttr.setIsKeyAttr(strings[3]=="是"?"1":"0");
					productAttr.setName(strings[2]);
					productAttrBiz.add(productAttr);
					System.out.println(productAttr.getId());
					strings[2] = productAttr.getId().toString();
				}
			}
			//保存字典
			for (String[] strings : data) {
				System.out.println(strings[0]);
				strings[2] = strings[2].replaceAll("#id", "");
				ProductAttr attr = productAttrBiz.get(Long.valueOf(strings[2]));
				ProductAttrDict dict = new ProductAttrDict();
				dict.setProductAttr(attr);
				dict.setOriginalValue(strings[4]);//原始值
				dict.setStandardValue(strings[5]);//标准值
				dict.setShortValue(strings[6]);//简称
				productAttrDictBiz.add(dict);
			}
		}
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		out.write(message);
		out.flush();
		out.close();
	}
	public String getAttrGroupName() {
		return attrGroupName;
	}
	public void setAttrGroupName(String attrGroupName) {
		this.attrGroupName = attrGroupName;
	}
}
